<?php

function newAuthCode($id) {
	// Vygenerování klíče
	$klic = rand(0,9999);

	// Uložení klíče
	mysql_query("UPDATE uzivatel SET KLIC='$klic' WHERE ID_UZIVATEL='$id'");

	// Vygenerování kódu
	$query = mysql_query("SELECT HESLO FROM uzivatel WHERE ID_UZIVATEL='$id'");
	$fetch = MySQL_Fetch_Array($query);
	$heslo = $fetch["HESLO"];
	$kod = md5($heslo.$klic);

	return $kod;
}

function authorized($kod, $id) {
	// Načtení hesla a klíče z databáze
	$query = mysql_query("SELECT HESLO, KLIC, ROLE FROM uzivatel WHERE ID_UZIVATEL='$id'");
	if(mysql_num_rows($query) == 0) return false;
	$fetch = MySQL_Fetch_Array($query);
	$db_heslo = $fetch["HESLO"];
	$db_klic = $fetch["KLIC"];
	 
	// Vygenerování kódu
	$spravny_kod = md5($db_heslo.$db_klic);
	 
	// Test správnosti kódu
	return ($spravny_kod == $kod);
}

function addVisitor(){
	mysql_query("UPDATE pocitadlo SET POCET=POCET+1");
}

function getUser($id){
	return mysql_query("select * from uzivatel where ID_UZIVATEL='$id'");
}

function getTabulka($trida, $sort1, $asc1, $sort2, $asc2, $sort3, $asc3, $season, $from, $to){
	$order = "order by ";
	if($sort1 != "") {
		$order .= $sort1;
		if ($asc1 == 1) $order .= " asc, ";
		else $order .= " desc, ";

		if($sort2 != "") {
			$order .= $sort2;
			if ($asc2 == 1) $order .= " asc, ";
			else $order .= " desc, ";

			if($sort3 != "") {
				$order .= $sort3;
				if ($asc3 == 1) $order .= " asc, ";
				else $order .= " desc, ";
			}
		}
	}
	$order .= " m.nazev ASC";
	$utkani_criteria =  "w.trida='$trida' and w.soutezni='1' and w.odehrano='1' and w.sezona='$season' and w.kolo>='$from' and w.kolo<='$to'";

	$query = "select m.nazev as tym, m.zdelov as is_zdelov,
                IFNULL(SUM(u.b_domaci>u.b_hoste and u.id_domaci=m.id_muzstvo), 0) as v_doma, 
								IFNULL(SUM(u.b_domaci=u.b_hoste and u.id_domaci=m.id_muzstvo), 0) as r_doma, 
								IFNULL(SUM(u.b_domaci<u.b_hoste and u.id_domaci=m.id_muzstvo), 0) as p_doma, 
								IFNULL(SUM(u.b_hoste>u.b_domaci and u.id_hoste=m.id_muzstvo), 0) as v_venku, 
								IFNULL(SUM(u.b_hoste=u.b_domaci and u.id_hoste=m.id_muzstvo), 0) as r_venku, 
								IFNULL(SUM(u.b_hoste<u.b_domaci and u.id_hoste=m.id_muzstvo), 0) as p_venku, 
								IFNULL(SUM(u.b_domaci>u.b_hoste and u.id_domaci=m.id_muzstvo)+SUM(u.b_hoste>u.b_domaci and u.id_hoste=m.id_muzstvo), 0) as v_celkem, 
								IFNULL(SUM(u.b_domaci=u.b_hoste and u.id_domaci=m.id_muzstvo)+SUM(u.b_hoste=u.b_domaci and u.id_hoste=m.id_muzstvo), 0) as r_celkem, 
								IFNULL(SUM(u.b_domaci<u.b_hoste and u.id_domaci=m.id_muzstvo)+SUM(u.b_hoste<u.b_domaci and u.id_hoste=m.id_muzstvo), 0) as p_celkem, 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) as gv_doma, 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) as go_doma, 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) - 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) as dif_doma, 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as gv_venku, 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as go_venku, 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) - 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as dif_venku, 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) + 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as gv_celkem, 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) + 
							  (select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as go_celkem,  
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) + 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) - 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) - 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as dif_celkem,  
								IFNULL(SUM(3*(u.b_domaci>u.b_hoste and u.id_domaci=m.id_muzstvo) 
										 +(u.b_domaci=u.b_hoste  and u.id_domaci=m.id_muzstvo)), 0) as b_doma, 
								IFNULL(SUM(3*(u.b_hoste>u.b_domaci and u.id_hoste=m.id_muzstvo) 
										 +(u.b_domaci=u.b_hoste  and u.id_hoste=m.id_muzstvo)), 0) as b_venku, 
								IFNULL(SUM( 
									  3*(u.b_domaci>u.b_hoste and u.id_domaci=m.id_muzstvo) + 
										  (u.b_domaci=u.b_hoste  and u.id_domaci=m.id_muzstvo) + 
										3*(u.b_hoste>u.b_domaci and u.id_hoste=m.id_muzstvo) + 
 										  (u.b_domaci=u.b_hoste  and u.id_hoste=m.id_muzstvo) ), 0) as b_celkem 
                from muzstvo m
                join utkani u
                where m.trida='$trida' and u.trida='$trida' and u.soutezni='1' and u.odehrano='1' and u.sezona='$season' and u.kolo>='$from' and u.kolo<='$to'
                group by m.id_muzstvo $order";
	return mysql_query($query);
}

function getSimpleTable(){
	$order = "order by b_celkem desc, dif_celkem desc, gv_celkem desc, m.nazev ASC";
	$utkani_criteria =  "w.trida='".DEFAULT_LEAGUE."' and w.soutezni='1' and w.odehrano='1' and w.sezona='".SEASON."' and w.kolo>='1' and w.kolo<='26'";

	$query = "select m.nazev as tym, m.zdelov as is_zdelov,
                IFNULL(SUM(u.id_hoste=m.id_muzstvo OR u.id_domaci=m.id_muzstvo), 0) AS u_celkem, 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) + 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as gv_celkem,  
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) + 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) - 
								(select IFNULL(SUM(w.b_hoste), 0) from utkani w where w.id_domaci=m.id_muzstvo and $utkani_criteria) - 
								(select IFNULL(SUM(w.b_domaci), 0) from utkani w where w.id_hoste=m.id_muzstvo and $utkani_criteria) as dif_celkem,  
								IFNULL(SUM( 
									  3*(u.b_domaci>u.b_hoste and u.id_domaci=m.id_muzstvo) + 
										  (u.b_domaci=u.b_hoste  and u.id_domaci=m.id_muzstvo) + 
										3*(u.b_hoste>u.b_domaci and u.id_hoste=m.id_muzstvo) + 
 										  (u.b_domaci=u.b_hoste  and u.id_hoste=m.id_muzstvo) ), 0) as b_celkem 
                FROM muzstvo m
                JOIN utkani u
                WHERE m.trida='".DEFAULT_LEAGUE."' and u.trida='".DEFAULT_LEAGUE."' and u.soutezni='1' and u.odehrano='1' and u.sezona='".SEASON."' and u.kolo>='1' and u.kolo<='26'
                GROUP by m.id_muzstvo $order";
	return mysql_query($query);
}

function getPlayersStats($sortby, $ascdesc, $season){
	$order = "";
	if($sortby != "") {
		$order .= "order by ".$sortby;
		if ($ascdesc == 0) $order .= " asc";
		else $order .= " desc";
	}

	$query = "SELECT h.jmeno as jmeno, h.prijmeni as prijmeni, (FLOOR(DATEDIFF(NOW(), h.narozen)/365)) as vek,
			IFNULL(h.narozen, NOW()) as datum, h.id_hrac as id_hrace, h.foto as foto, h.hostovani as hostovani, h.narozen as narozen,
			(select count(*) from soupiska s join utkani u where s.id_hrac=h.id_hrac and s.id_utkani=u.id_utkani and u.sezona='$season' and u.soutezni='1') as utkani,
			(select p.zkratka from post p where p.id_post=h.id_post) as post,
			(select p.nazev from post p where p.id_post=h.id_post) as post_long,
			(select IFNULL(sum(s.branky), 0) from soupiska s join utkani u where s.id_hrac=h.id_hrac and s.id_utkani=u.id_utkani and u.sezona='$season' and u.soutezni='1') as branky,
			(select IFNULL(sum(s.zk), 0) from soupiska s join utkani u where s.id_hrac=h.id_hrac and s.id_utkani=u.id_utkani and u.sezona='$season' and u.soutezni='1') as zk,
			(select IFNULL(sum(s.ck), 0) from soupiska s join utkani u where s.id_hrac=h.id_hrac and s.id_utkani=u.id_utkani and u.sezona='$season' and u.soutezni='1') as ck
      FROM hrac h WHERE h.aktivni='1' $order";

	return mysql_query($query);
}

function getPlayers(){
	$query = "select h.jmeno as jmeno, h.prijmeni as prijmeni, TIMESTAMPDIFF(YEAR,h.narozen, NOW()) as vek,
			h.id_hrac as id_hrace, h.aktivni as aktivni, h.svatek as svatek,
			(select p.zkratka from post p where p.id_post = h.id_post) as post
			from hrac h order by h.aktivni desc, h.prijmeni asc, h.jmeno asc";
	return mysql_query($query);
}

function getAllPlayers() {
	$query = "select h.jmeno as jmeno, h.prijmeni as prijmeni,
			h.id_hrac as id_hrace from hrac h order by h.prijmeni asc, h.jmeno asc";
	return mysql_query($query);
}

function getActivePlayers(){
	$query = "select h.jmeno as jmeno, h.prijmeni as prijmeni,
			h.id_hrac as id_hrace from hrac h where h.aktivni='1' order by h.prijmeni asc, h.jmeno asc";
	return mysql_query($query);
}

function getVysledkySoutez($muzstvo, $sezona){
	$query = "select u.id_clanek as id_clanek, u.datum as datum, DAYOFWEEK(u.datum) as den, u.b_domaci as gd, u.b_hoste as gh,
        u.odehrano as odehrano, u.soutezni as mistrak, u.kolo as kolo,
				(select m.nazev from muzstvo m where m.id_muzstvo=u.id_domaci) as domaci,
				(select m.nazev from muzstvo m where m.id_muzstvo=u.id_hoste) as hoste
        FROM utkani u where (u.id_domaci='$muzstvo' OR u.id_hoste='$muzstvo') and u.soutezni='1' AND u.sezona='$sezona' ORDER BY u.kolo ASC";

	return mysql_query($query);
}

function getVysledkyPratelske($muzstvo, $sezona){
	$query = "select u.id_clanek as id_clanek, u.datum as datum, DAYOFWEEK(u.datum) as den, u.b_domaci as gd, u.b_hoste as gh,
        u.odehrano as odehrano, u.soutezni as mistrak, u.poznamka as poznamka,
				(select m.nazev from muzstvo m where m.id_muzstvo=u.id_domaci) as domaci,
				(select m.nazev from muzstvo m where m.id_muzstvo=u.id_hoste) as hoste
          from utkani u where (u.id_domaci='$muzstvo' or u.id_hoste='$muzstvo') and u.soutezni='0' AND u.sezona='$sezona' ORDER BY u.datum ASC";

	return mysql_query($query);
}

function getPosts(){
	return mysql_query("SELECT id_post, nazev, zkratka FROM post");
}

function userExists($username){
	$query = mysql_query("select count(*) as pocet FROM uzivatel WHERE LOWER(NICK)=LOWER('$username')");
	$fetch = MySQL_Fetch_Array($query);
	if($fetch["pocet"] > 0) return true;
	return false;
}

function setRole($user_id, $user_role) {
	mysql_query("UPDATE uzivatel SET ROLE='$user_role' WHERE ID_UZIVATEL='$user_id'");
}

function userLogin($username, $pass) {
	$query = mysql_query("SELECT count(*) AS pocet FROM uzivatel WHERE nick='$username' AND heslo='$pass'");
	$fetch = MySQL_Fetch_Array($query);
	if($fetch["pocet"] > 0) {
		mysql_query("UPDATE uzivatel SET POSLEDNI_LOGIN=NOW() WHERE nick='$username'");
		return true;
	}
	return false;
}

function registerUser($username, $pass, $name, $surname, $email, $activation){
	mysql_query("INSERT INTO uzivatel (nick, heslo, role, jmeno, prijmeni, email, datum_registrace, aktivacni_kod, posledni_zmena) VALUES('$username', '$pass',
        '".ROLE_USER."', '$name', '$surname', '$email', NOW(), '$activation', NOW())");
}

function canEditBasicInfo($id_user) {
	list($can_edit) = mysql_fetch_row( mysql_query("SELECT IF(TO_DAYS(NOW()) - TO_DAYS(IFNULL(POSLEDNI_ZMENA, SUBDATE(NOW(), INTERVAL 31 DAY))) < 30, false, true) as editable FROM uzivatel WHERE ID_UZIVATEL='$id_user'") );
	return $can_edit;
}

function updateUserBasic($id_user, $ch_username, $ch_name, $ch_surname) {
	mysql_query("UPDATE uzivatel SET NICK='$ch_username', JMENO='$ch_name', PRIJMENI='$ch_surname', POSLEDNI_ZMENA=NOW() 
			WHERE ID_UZIVATEL='$id_user'");
}

function updateUserOther($id_user, $ch_email) {
	mysql_query("UPDATE uzivatel SET EMAIL='$ch_email' WHERE ID_UZIVATEL='$id_user'");
}

function changePassword($id_user, $pass){
	mysql_query("UPDATE uzivatel SET heslo='$pass' WHERE ID_UZIVATEL='$id_user'");
}

function getMatchesList() {
	return mysql_query("SELECT u.id_utkani as id_utkani, u.datum as datum, u.b_domaci as b_domaci, u.b_hoste as b_hoste,
  	         (select nazev from muzstvo  m where m.id_muzstvo=u.id_domaci) as domaci,
  	         (select nazev from muzstvo  m where m.id_muzstvo=u.id_hoste) as hoste
              FROM utkani u WHERE (u.id_domaci=".ZDELOV_A." OR u.id_hoste=".ZDELOV_A.") AND u.odehrano='1' ORDER BY u.datum desc");
}

function getUserIdByActivation($code) {
	list($id) = mysql_fetch_row( mysql_query("SELECT ID_UZIVATEL FROM uzivatel WHERE AKTIVACNI_KOD='$code'") );
	return $id;
}

function isAccountActivated($user_id){
	list($active) = mysql_fetch_row( mysql_query("SELECT aktivni FROM uzivatel WHERE id_uzivatel='$user_id'") );
	return $active;
}

function activateAccount($user_id){
	mysql_query("UPDATE uzivatel SET aktivni='1', aktivacni_kod='' WHERE id_uzivatel='$user_id' AND aktivni='0'");
}

function getUserId($username){
	list($id) = mysql_fetch_row( mysql_query("select ID_UZIVATEL from uzivatel where nick='$username'") );
	return $id;
}

function getUserByUsername($username){
	list($id, $nick, $role) = mysql_fetch_row( mysql_query("select ID_UZIVATEL, NICK, ROLE from uzivatel where NICK='$username'") );
	return new User($id, $nick, $role);
}

function getUsername($id){
	list($username) = mysql_fetch_row( mysql_query("select NICK from uzivatel where ID_UZIVATEL='$id'") );
	return $username;
}

function getPassword($id){
	list($pass) = mysql_fetch_row( mysql_query("select HESLO from uzivatel where ID_UZIVATEL='$id'") );
	return $pass;
}

function getRole($id){
	list($role) = mysql_fetch_row( mysql_query("select ROLE from uzivatel where ID_UZIVATEL='$id'") );
	return $role;
}

function getPlayer($player_id){
	return mysql_query("SELECT * FROM hrac WHERE ID_HRAC='$player_id'");
}

function getTeam($team_id){
	return mysql_query("SELECT * FROM muzstvo WHERE ID_MUZSTVO='$team_id'");
}

function getVidea(){
	return mysql_query("SELECT * FROM video ORDER BY DATUM DESC");
}

function getVideo($video_id){
	return mysql_query("SELECT * FROM video WHERE ID='$video_id'");
}

function getVideaByReport($report_id) {
	return mysql_query("SELECT * FROM video WHERE ID_CLANEK='$report_id'");
}

function getResult($utkani_id){
	return mysql_query("SELECT *,
                      (SELECT TRIDA FROM muzstvo where ID_MUZSTVO = u.ID_DOMACI) as trida_domaci,
                      (SELECT TRIDA FROM muzstvo where ID_MUZSTVO = u.ID_HOSTE) as trida_hoste
                      FROM utkani u WHERE u.ID_UTKANI='$utkani_id'");
}

function addCriteria($criteria, $col_name, $expr, $value){
	if($criteria == ""){
		$criteria = "WHERE ".$col_name.$expr."'".$value."'";
	} else {
		$criteria .= " AND ".$col_name.$expr."'".$value."'";
	}
	return $criteria;
}

function createCriteriaForResults($filter){
	if($filter == "") return "";
	$criteria = "";
	if($filter->kolo != "") $criteria = addCriteria($criteria, "u.KOLO", "=", $filter->kolo);
	if($filter->datum_od != "") $criteria = addCriteria($criteria, "DATE(u.DATUM)", ">=", $filter->datum_od);
	if($filter->datum_do != "") $criteria = addCriteria($criteria, "DATE(u.DATUM)", "<=", $filter->datum_do);
	if($filter->trida != "") $criteria = addCriteria($criteria, "u.TRIDA", "<=", $filter->trida);
	if($filter->soutezni != "") $criteria = addCriteria($criteria, "u.SOUTEZNI", "<=", $filter->soutezni);
	if($filter->sezona != "") $criteria = addCriteria($criteria, "u.SEZONA", "=", $filter->sezona);
	return $criteria;
}

function getResultsCount($filter){
	$criteria = createCriteriaForResults($filter);
	return mysql_query("SELECT count(*) as POCET FROM utkani u $criteria");
}

function getResultsFromTo($from, $to, $filter){
	$criteria = createCriteriaForResults($filter);
	return mysql_query("SELECT u.id_utkani as id_utkani, u.datum as datum, u.kolo as kolo,
              u.b_domaci as b_domaci, u.b_hoste as b_hoste, u.soutezni as soutezni,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_domaci) as domaci,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_hoste) as hoste
        FROM utkani u $criteria ORDER BY u.datum DESC, u.kolo DESC LIMIT $from, $to");
}

function getResults($filter){
	$criteria = createCriteriaForResults($filter);
	return mysql_query("SELECT u.id_utkani as id_utkani, u.datum as datum, u.kolo as kolo,
              u.b_domaci as b_domaci, u.b_hoste as b_hoste, u.soutezni as soutezni,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_domaci) as domaci,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_hoste) as hoste
        FROM utkani u $criteria ORDER BY u.datum ASC, u.kolo DESC");
}

function getRounds($sezona){
	return mysql_query("SELECT u.kolo as kolo, u.datum as datum FROM utkani u WHERE u.soutezni='1'
          AND u.sezona='$sezona' AND (u.kolo!=NULL OR u.kolo !='') GROUP BY u.kolo ORDER BY u.kolo ASC, u.datum ASC");
}

function getMatchesWithPlayers(){
	return mysql_query("SELECT u.id_utkani as id_utkani, u.datum as datum,
              u.b_domaci as b_domaci, u.b_hoste as b_hoste,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_domaci) as domaci,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_hoste) as hoste
        FROM utkani u JOIN soupiska s ON (u.id_utkani = s.id_utkani) WHERE u.id_hoste=".ZDELOV_A." OR u.id_domaci=".ZDELOV_A."
        GROUP BY u.id_utkani ORDER BY u.datum desc");
}

function getMatchesPlayed(){
	return mysql_query("SELECT u.id_utkani as id_utkani, u.datum as datum,
              u.b_domaci as b_domaci, u.b_hoste as b_hoste,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_domaci) as domaci,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_hoste) as hoste
        FROM utkani u WHERE u.odehrano='1' AND (u.id_hoste=".ZDELOV_A." OR u.id_domaci=".ZDELOV_A.") 
        ORDER BY u.datum desc");
}

function getPostId($player_id){
	$query = mysql_query("SELECT ID_POST FROM hrac WHERE ID_HRAC='$player_id'");
	$fetch = MySQL_Fetch_Array($query);
	return $fetch["ID_POST"];
}

function activateUser($username, $pass){
	$query = mysql_query("select count(*) as pocet from uzivatel where nick='$username'");
	$fetch = MySQL_Fetch_Array($query);
	if($fetch["pocet"] > 0) {
		$query = mysql_query("UPDATE uzivatel SET aktivni='1'");
		return true;
	}
	return false;
}

function insertReport($nadpis, $popis, $obsah, $autor_id){
	$query = mysql_query("INSERT INTO clanek (CAS_VYTVORENI, CAS_UPRAVY, NADPIS, POPIS, TEXT, ID_AUTOR) VALUES(NOW(), NOW(), '$nadpis'
              , '$popis', '$obsah', '$autor_id')");
	$query = mysql_query("SELECT id_clanku FROM clanek ORDER BY id_clanku DESC LIMIT 1");
	$fetch = MySQL_Fetch_Array($query);
	return $fetch["id_clanku"];
}

function setReportMatch($a_cl_id_utkani, $id_clanku){
	mysql_query("UPDATE utkani SET ID_CLANEK='$id_clanku' WHERE ID_UTKANI='$a_cl_id_utkani'");
}

function updateReport($update, $id_clanku, $a_cl_nadpis, $a_cl_popis, $a_cl_obsah, $a_cl_autor_id) {
	if($update) {
		mysql_query("UPDATE clanek SET CAS_UPRAVY=NOW(), NADPIS='$a_cl_nadpis', 
                  POPIS='$a_cl_popis', TEXT='$a_cl_obsah', ID_AUTOR='$a_cl_autor_id' WHERE id_clanku='$id_clanku'");
	} else {
		mysql_query("UPDATE clanek SET NADPIS='$a_cl_nadpis', 
                  POPIS='$a_cl_popis', TEXT='$a_cl_obsah' WHERE id_clanku='$id_clanku'");
	}
	
}

function insertForumTheme($tema, $autor) {
	mysql_query("INSERT INTO diskuze_tema (CAS, NAZEV, ID_AUTOR, AUTORIZACE)
                          VALUES(NOW(), '$tema', '$autor', '0')");
	$query = mysql_query("SELECT ID_TEMA FROM diskuze_tema WHERE NAZEV='$tema'");
	$fetch = MySQL_Fetch_Array($query);
	return $fetch["ID_TEMA"];
}

function insertForumComment($id_tema, $autor, $comment) {
	$query = mysql_query("INSERT INTO diskuze_prispevek (ID_TEMA, ID_AUTOR, CAS, PRISPEVEK)
                          VALUES('$id_tema', '$autor', NOW(), '$comment')");
}

function insertReportComment($id_clanku, $id_user, $komentar_text){
	mysql_query("INSERT INTO komentar (ID_CLANKU, ID_AUTOR, CAS, TEXT)
                        VALUES('$id_clanku', '$id_user', NOW(), '$komentar_text')");
}

function insertPlayer($name, $surname, $birthday, $nameday, $hosting, $post_id, $active, $photo){
	$query = mysql_query("INSERT INTO hrac (JMENO, PRIJMENI, NAROZEN, SVATEK, HOSTOVANI, ID_POST, AKTIVNI, FOTO)
      VALUES('$name', '$surname', '$birthday', '$nameday', '$hosting', '$post_id', '$active', '$photo')");
}

function updatePlayer($player_id, $name, $surname, $birthday, $nameday, $hosting, $post_id, $active, $photo){
	mysql_query("update hrac set JMENO='$name', PRIJMENI='$surname', NAROZEN='$birthday', SVATEK='$nameday'
              , AKTIVNI='$active', ID_POST='$post_id', FOTO='$photo'
              , HOSTOVANI='$hosting' WHERE ID_HRAC='$player_id'");
}

function getPlayerName($player_id) {
	list($name, $surname) = mysql_fetch_row( mysql_query("SELECT JMENO, PRIJMENI FROM hrac where ID_HRAC='$player_id'") );
	return $name." ".$surname;
}

function deletePlayer($player_id) {
	mysql_query("DELETE FROM hrac WHERE ID_HRAC='$player_id'");
}

function deleteUser($user_id) {
	mysql_query("DELETE FROM uzivatel WHERE ID_UZIVATEL='$user_id'");
}

function insertTeam($nazev, $zkratka, $trida){
	$query = mysql_query("INSERT INTO muzstvo (NAZEV, ZKRATKA, TRIDA)
      VALUES('$nazev', '$zkratka', '$trida')");
}

function updateTeam($team_id, $nazev, $zkratka, $trida) {
	mysql_query("update muzstvo set NAZEV='$nazev', ZKRATKA='$zkratka', TRIDA='$trida'
              WHERE ID_MUZSTVO='$team_id'");
}

function deleteTeam($team_id) {
	mysql_query("DELETE FROM muzstvo WHERE ID_MUZSTVO='$team_id'");
}

function insertVideo($nazev, $datum, $soubor, $id_clanek){
	$query = mysql_query("INSERT INTO video (NAZEV, DATUM, SOUBOR, ID_CLANEK)
      VALUES('$nazev', '$datum', '$soubor', '$id_clanek')");
}

function updateVideo($video_id, $nazev, $datum, $soubor, $id_clanek){
	mysql_query("UPDATE video SET NAZEV='$nazev', DATUM='$datum', SOUBOR='$soubor', ID_CLANEK='$id_clanek'
              WHERE ID='$video_id'");
}

function deleteVideo($video_id){
	mysql_query("DELETE FROM video WHERE ID='$video_id'");
}

function deleteClen($clen_id){
	mysql_query("DELETE FROM lide WHERE ID_CLOVEK='$clen_id'");
}

function insertSoupiska($id_utkani, $id_hrac, $id_post, $b, $zk, $ck, $stridal){
	mysql_query("INSERT INTO soupiska (ID_UTKANI, ID_HRAC, ID_POST, BRANKY, ZK, CK, STRIDAL)
      VALUES('$id_utkani', '$id_hrac', '$id_post', '$b', '$zk', '$ck', '$stridal')");
}

function getSoupiskas($id_utkani){
	return mysql_query("SELECT * FROM soupiska WHERE ID_UTKANI='$id_utkani' AND STRIDAL='-1' ORDER BY ID_SOUPISKA ASC");
}

function bylStridan($id_hrac, $id_utkani){
	$query = mysql_query("SELECT ID_HRAC FROM soupiska WHERE ID_UTKANI='$id_utkani' AND STRIDAL='$id_hrac'");
	if(mysql_num_rows($query) > 0) return true;
	return false;
}

function getStridajici($id_hrac, $id_utkani){
	return mysql_query("SELECT * FROM soupiska WHERE ID_UTKANI='$id_utkani' AND STRIDAL='$id_hrac'");
}

function getClen($id_clen){
	return mysql_query("SELECT * FROM lide WHERE ID_CLOVEK='$id_clen'");
}

function insertClen($name, $function, $priority, $photo){
	mysql_query("INSERT INTO lide (JMENO, FUNKCE, PRIORITA, FOTO)
      VALUES('$name', '$function', '$priority', '$team', '$photo')");
}

function updateClen($human_id, $name, $function, $priority, $photo){
	mysql_query("UPDATE lide SET JMENO='$name', FUNKCE='$function', PRIORITA='$priority',
              FOTO='$photo' WHERE ID_CLOVEK='$human_id'");
}

function getUzivatele(){
	return mysql_query("SELECT * FROM uzivatel ORDER BY posledni_login DESC");
}

function getTeams($soutez){
	if($soutez != ""){
		return mysql_query("SELECT * FROM muzstvo WHERE TRIDA='$soutez' ORDER BY nazev ASC");
	} else {
		return mysql_query("SELECT * FROM muzstvo ORDER BY nazev ASC");
	}
}

function getClankyCount(){
	return mysql_query("SELECT count(*) as POCET FROM clanek");
}

function getClanky($from, $count){
	return mysql_query("SELECT * FROM clanek ORDER BY CAS_UPRAVY DESC LIMIT $from, $count");
}

function getAllClanky() {
	return mysql_query("SELECT * FROM clanek ORDER BY CAS_UPRAVY DESC");
}

function getAllAktualizace() {
	return mysql_query("SELECT * FROM aktualizace ORDER BY DATUM DESC");
}

function getAktualizace($id){
	return mysql_query("SELECT * FROM aktualizace WHERE ID_AKTUALIZACE='$id'");
}

function insertAktualizace($version, $content) {
	mysql_query("INSERT INTO aktualizace (VERZE, OBSAH, DATUM)
      VALUES('$version', '$content', NOW())");
}

function updateAktualizace($id, $version, $content){
	mysql_query("UPDATE aktualizace SET VERZE='$version', OBSAH='$content' WHERE ID_AKTUALIZACE='$id'");
}

function deleteAktualizace($id){
	mysql_query("DELETE FROM aktualizace WHERE ID_AKTUALIZACE='$id'");
}

function getZpravy($from, $count){
	return mysql_query("SELECT *,(SELECT count(*) FROM komentar k WHERE k.ID_CLANKU=cl.ID_CLANKU) as komentaru
               FROM clanek cl ORDER BY CAS_UPRAVY DESC LIMIT $from, $count");
}

function insertUtkani($a_vysledek_datum, $a_vysledek_id_domaci, $a_vysledek_id_hoste, $a_vysledek_b_domaci,
		$a_vysledek_b_hoste, $soutezni, $a_vysledek_sezona, $a_vysledek_odehrano, $a_vysledek_kolo,
		$a_vysledek_pozn, $a_vysledek_id) {
	mysql_query("INSERT INTO utkani (DATUM, ID_DOMACI, ID_HOSTE, B_DOMACI, B_HOSTE, ODEHRANO, SOUTEZNI, TRIDA, SEZONA, KOLO, POZNAMKA) 
	              VALUES('$a_vysledek_datum', '$a_vysledek_id_domaci', '$a_vysledek_id_hoste', 
	                   $a_vysledek_b_domaci, $a_vysledek_b_hoste,'$a_vysledek_odehrano','$soutezni',
	                   '2','$a_vysledek_sezona', $a_vysledek_kolo, '$a_vysledek_pozn')");
}

function updateUtkani($a_vysledek_datum, $a_vysledek_id_domaci, $a_vysledek_id_hoste, $a_vysledek_b_domaci,
		$a_vysledek_b_hoste, $soutezni, $a_vysledek_sezona, $a_vysledek_odehrano, $a_vysledek_kolo,
		$a_vysledek_pozn, $a_vysledek_id) {
	mysql_query("UPDATE utkani SET DATUM='$a_vysledek_datum', ID_DOMACI='$a_vysledek_id_domaci', 
	             ID_HOSTE='$a_vysledek_id_hoste', B_DOMACI=$a_vysledek_b_domaci, B_HOSTE=$a_vysledek_b_hoste,
	             SOUTEZNI='$soutezni', SEZONA='$a_vysledek_sezona', ODEHRANO='$a_vysledek_odehrano',
	             KOLO=$a_vysledek_kolo, POZNAMKA='$a_vysledek_pozn' WHERE ID_UTKANI='$a_vysledek_id'");
}

function getIdUtkaniForClanek($id_clanku){
	$query = mysql_query("SELECT ID_UTKANI FROM utkani WHERE ID_CLANEK='$id_clanku'");
	if(mysql_num_rows($query) == 0) return -1;
	$fetch = MySQL_Fetch_Array($query);
	return $fetch["ID_UTKANI"];
}

function getNextCelebrations($count){
	$query = mysql_query("SELECT *, IF( DAYOFYEAR(narozen) >= DAYOFYEAR(NOW()),
		    DAYOFYEAR(narozen) - DAYOFYEAR(NOW()),
		    365 + DAYOFYEAR(narozen) - DAYOFYEAR(NOW())) AS zbyva
		    FROM hrac WHERE AKTIVNI='1' AND NAROZEN != '0000-00-00' ORDER BY zbyva ASC LIMIT 0,$count");

	$oslavy = new Oslavy();
	while($fetch = MySQL_Fetch_Array($query)) {
		$jmeno = $fetch["PRIJMENI"]." ".$fetch["JMENO"];
		$datum = DateFormat::format($fetch["NAROZEN"], DF_DDMM);
		$zbyva = $fetch["zbyva"];
		$co = "narozeniny";
		$oslavy->pridat($datum, $jmeno, $co, $zbyva);
	}

	$query = mysql_query("SELECT *, IF( DAYOFYEAR(svatek) >= DAYOFYEAR(NOW()),
		    DAYOFYEAR(svatek) - DAYOFYEAR(NOW()),
		    365 + DAYOFYEAR(svatek) - DAYOFYEAR(NOW())) AS zbyva
		    FROM hrac WHERE AKTIVNI='1' AND SVATEK != '0000-00-00' ORDER BY zbyva ASC LIMIT 0,$count");

	while($fetch = MySQL_Fetch_Array($query)) {
		$jmeno = $fetch["PRIJMENI"]." ".$fetch["JMENO"];
		$datum = DateFormat::format($fetch["SVATEK"], DF_DDMM);
		$zbyva = $fetch["zbyva"];
		$co = "svátek";
		$oslavy->pridat($datum, $jmeno, $co, $zbyva);
	}
	return $oslavy;
}

function getVideos() {
	return mysql_query("SELECT * FROM video ORDER BY DATUM DESC");
}

function getLastFixture() {
	return mysql_query("SELECT u.datum as datum, DAYOFWEEK(u.datum) as den,
              u.b_domaci as b_domaci, u.b_hoste as b_hoste,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_domaci) as domaci,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_hoste) as hoste
        FROM utkani u WHERE (u.id_hoste=1 OR u.id_domaci=1) AND u.odehrano=1 ORDER BY u.datum desc LIMIT 0,1");
}

function getNextFixture() {
	return mysql_query("SELECT u.datum as datum, DAYOFWEEK(u.datum) as den,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_domaci) as domaci,
	           (select nazev from muzstvo  m where m.id_muzstvo=u.id_hoste) as hoste
        FROM utkani u WHERE (u.id_hoste=1 OR u.id_domaci=1) AND u.odehrano=0 ORDER BY u.datum asc LIMIT 0,1");
}

function getForumsCount($forum_title){
	$query = mysql_query("SELECT count(*) as pocet FROM diskuze_tema WHERE NAZEV='".$forum_title."'");
	$fetch = mysql_fetch_array($query);
	return $fetch["pocet"];
}

function getForumThemeTitle($id){
	return mysql_query("SELECT NAZEV FROM diskuze_tema WHERE ID_TEMA='".$id."'");
}

function getForumThemes() {
	return mysql_query("SELECT * FROM (
								SELECT dt.ID_TEMA, dt.NAZEV,
				        (SELECT count(*) FROM diskuze_prispevek dip WHERE dip.ID_TEMA=dt.ID_TEMA) AS pocet,
                (SELECT dip.CAS FROM diskuze_prispevek dip WHERE dip.ID_TEMA=dt.ID_TEMA ORDER BY dip.CAS DESC LIMIT 1) AS posl_cas,
                (SELECT dip.ID_PRISPEVEK FROM diskuze_prispevek dip WHERE dip.ID_TEMA=dt.ID_TEMA ORDER BY dip.CAS DESC LIMIT 1) AS posl_id,
                (SELECT u.NICK FROM uzivatel u JOIN diskuze_prispevek dip WHERE dip.ID_AUTOR = u.ID_UZIVATEL AND dip.ID_TEMA=dt.ID_TEMA ORDER BY dip.CAS DESC LIMIT 1) AS posl_nick
                FROM diskuze_tema dt 
								LEFT JOIN diskuze_prispevek dp 
								ON (dt.ID_TEMA=dp.ID_TEMA) 
                GROUP BY dt.ID_TEMA				
							) as seznam ORDER BY seznam.posl_cas DESC");
}

function getForumComments($theme_id){
	return mysql_query("SELECT dip.*, u.NICK as nick FROM diskuze_prispevek dip LEFT JOIN uzivatel u ON (dip.ID_AUTOR = u.ID_UZIVATEL) WHERE ID_TEMA='$theme_id' ORDER BY dip.CAS ASC");
}

function getReportAndPlayersCount($id_clanku){
	return mysql_query("SELECT cl.*, u.ID_UTKANI as id_utkani,
                      (SELECT count(*) FROM soupiska s WHERE s.ID_UTKANI=u.ID_UTKANI) as pocet_hracu,
                      (SELECT NICK FROM uzivatel WHERE ID_UZIVATEL = cl.ID_AUTOR) as autor
                      FROM clanek cl LEFT JOIN utkani u ON(u.ID_CLANEK=cl.ID_CLANKU) WHERE cl.ID_CLANKU='$id_clanku'");
}

function getVisitorsCount() {
	return mysql_query("SELECT POCET FROM pocitadlo");
}

function getReportComments($id_clanku) {
	return mysql_query("SELECT u.NICK AS autor, k.CAS AS time, k.TEXT AS text, k.ID_KOMENTARE AS id FROM komentar k LEFT JOIN uzivatel u ON(k.ID_AUTOR = u.ID_UZIVATEL) WHERE k.ID_CLANKU='$id_clanku' ORDER BY k.CAS ASC");
}

function getReportTitle($id_clanku) {
	list($title) = mysql_fetch_row(mysql_query("SELECT NADPIS FROM clanek WHERE ID_CLANKU='$id_clanku'"));
	return $title;
}

function getReport($id_clanku) {
	return mysql_query("SELECT * FROM clanek WHERE ID_CLANKU='$id_clanku'");
}

function getCountReportComments($id_clanku) {
	list($count) = mysql_fetch_row(mysql_query("SELECT count(*) AS pocet FROM komentar k WHERE k.ID_CLANKU='$id_clanku'"));
	return $count;
}

function getReportVideos($id_clanku) {
	return mysql_query("SELECT * FROM video WHERE ID_CLANEK='".$id_clanku."'");
}

function getManagementTeam(){
	return mysql_query("SELECT * FROM lide ORDER BY priorita ASC");
}

function getLastAdditions($count){
	$prispevky = new LastAdditions($count);

	$query = mysql_query("SELECT k.CAS AS cas, k.ID_KOMENTARE as id, k.ID_CLANKU as id2, u.NICK as jmeno FROM komentar k LEFT JOIN uzivatel u ON(k.ID_AUTOR = u.ID_UZIVATEL) ORDER BY k.CAS DESC LIMIT 0,$count");

	while($fetch = MySQL_Fetch_Array($query)) {
		if(!$prispevky->add(new LastAddition($fetch["cas"], $fetch["jmeno"], COMM_TYPE_REPORT, $fetch["id"], $fetch["id2"]))) break;
	}
	 
	$query = mysql_query("SELECT dp.CAS AS cas, dp.ID_PRISPEVEK as id, dp.ID_TEMA as id2, u.NICK as jmeno FROM diskuze_prispevek dp LEFT JOIN uzivatel u ON(dp.ID_AUTOR = u.ID_UZIVATEL) ORDER BY dp.CAS DESC LIMIT 0,$count");

	while($fetch = MySQL_Fetch_Array($query)) {
		if(!$prispevky->add(new LastAddition($fetch["cas"], $fetch["jmeno"], COMM_TYPE_THEME_COMMENT, $fetch["id"], $fetch["id2"]))) break;
	}
	 
	$query = mysql_query("SELECT dt.CAS AS cas, dt.ID_TEMA as id, u.NICK as jmeno FROM diskuze_tema dt LEFT JOIN uzivatel u ON(dt.ID_AUTOR = u.ID_UZIVATEL) ORDER BY dt.CAS DESC LIMIT 0,$count");

	while($fetch = MySQL_Fetch_Array($query)) {
		if(!$prispevky->add(new LastAddition($fetch["cas"], $fetch["jmeno"], COMM_TYPE_NEW_THEME, $fetch["id"], ""))) break;
	}
	
	$query = mysql_query("SELECT a.DATUM AS cas, a.VERZE as verze FROM aktualizace a ORDER BY a.DATUM DESC LIMIT 0,$count");

	while($fetch = MySQL_Fetch_Array($query)) {
		if(!$prispevky->add(new LastAddition($fetch["cas"], $fetch["verze"], COMM_TYPE_UPDATE, "", ""))) break;
	}

	return $prispevky;
}

?>
